
//preloading.....
load("library/common.js");

JEuler.system.loadJS("library/math.js");

(function() {

    //JEuler.system.loadJS("data/???.dat");
    JEuler.system.printCaption("062");
    
    function getHash(num) {
    
        var hash=[], t1, t2, t3, sum=0;
        for(var i=0; i<=9; i++) { hash[i]=0; }
        for(var i=0; i<num.length; i++) {
            t1 = num[i]%10;
            t2 = (num[i]%100-t1)/10;
            t3 = (num[i]-t1-t2*10)/100;
            if(i!=num.length-1 || t3>0 || t2>0 || t1>0) {
                hash[t1] = (hash[t1] || 0) + 1;
                sum++;
            }
            if(i!=num.length-1 || t3>0 || t2>0) {
                hash[t2] = (hash[t2] || 0) + 1;
                sum++;
            }
            if(i!=num.length-1 || t3>0) {
                hash[t3] = (hash[t3] || 0) + 1;
                sum++;
            }
        }
        var hashValue=hash.join(",");
        return hashValue;
    }
    
    function getCube(n) {
        var cubeValues = [1], tmp, tm1p;
        
        for(var j=1; j<=3; j++) {
            tmp=0;
            for(var k=0; k<cubeValues.length; k++) {
                tmp = cubeValues[k] * n + tmp;
                cubeValues[k] = tmp%1000;
                tmp = (tmp-cubeValues[k])/1000;
            }
            while(tmp>0) {
                tmp1 = tmp%1000;
                cubeValues.push(tmp1);
                tmp = (tmp-tmp1)/1000;
            }            
        }
        return cubeValues;
    }
    
    JEuler.system.startTimerCount();
    
    var hashList=[], i=0, firstNumber=[];
    
    do {
        var hash = getHash(getCube(i));
        hashList[hash] = (hashList[hash] || 0) + 1;
        if(!firstNumber[hash]) firstNumber[hash] = i;
        if(hashList[hash]==5) {
            JEuler.system.print("Find....");
            JEuler.system.print(firstNumber[hash]);
            var cubeValues = getCube(firstNumber[hash]);
            JEuler.system.print(firstNumber[hash]+"^3="+cubeValues.reverse().map(
                function(a){
                    return (a+1000).toString().substr(1);
                }).join(""));
            break;
        }
        i++;
    } while(1);
    
    JEuler.system.printTimerCount();


})();
